package com.small.view.custview.dialog;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.small.R;

public class SingleChoicAdapter extends BaseAdapter implements
		OnItemClickListener {

	private List<String> mFirstList = new ArrayList<String>();
	private List<String> mSecondList = new ArrayList<String>();
	private int mCheckBoxResourceID = 0;
	public static int mSelectItem = -1;
	private LayoutInflater mInflater;
	ViewHolder viewHolder;
	Context cxt;
	Dialog mdialog;

	public SingleChoicAdapter(Dialog dialog, Context context, String[] itmes,
			int checkBoxResourceId) {
		init(context, checkBoxResourceId);
		mdialog = dialog;

		cxt = context;
		init(context, checkBoxResourceId);

		mFirstList = new ArrayList<String>();
		mSecondList = new ArrayList<String>();

		mFirstList.add("123");
		mSecondList.add("content");
		mFirstList.add("123");
		mSecondList.add("content");

		mSelectItem = 1;
	}

	private void init(Context context, int checkBoResourceId) {
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mCheckBoxResourceID = checkBoResourceId;
	}

	@Override
	public int getCount() {
		return mFirstList.size();
	}

	@Override
	public Object getItem(int position) {
		return mFirstList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public int getPosition(String item) {
		return mFirstList.indexOf(item);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.choice_single_list_item,
					null);
			viewHolder = new ViewHolder();
			viewHolder.viewParent = (View) convertView
					.findViewById(R.id.item_parent);
			viewHolder.mTextView = (TextView) convertView
					.findViewById(R.id.textView);
			viewHolder.mCheckBox = (CheckBox) convertView
					.findViewById(R.id.checkBox); 

			convertView.setTag(viewHolder);

			if (mCheckBoxResourceID != 0) {
				viewHolder.mCheckBox.setButtonDrawable(mCheckBoxResourceID);
			}

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		Log.d("mSelectItem", "" + mSelectItem);
		if (mSelectItem == position) {
			viewHolder.viewParent
					.setBackgroundResource(R.color.listitem_text_pressed);
			viewHolder.mCheckBox.setChecked(true);

		} else {
			viewHolder.viewParent
					.setBackgroundResource(R.color.listitem_color_noremal);
			viewHolder.mCheckBox.setChecked(false);
		}

		String item = (String) getItem(position);
		if (item instanceof CharSequence) {
			viewHolder.mTextView.setText((CharSequence) item);
		} else {
			viewHolder.mTextView.setText(item.toString());
		}

		return convertView;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		if (position != mSelectItem) {
			Log.d("mSelectItem", position + "");
			mSelectItem = position;
			notifyDataSetChanged();
		}

	}

	public static class ViewHolder {
		public View viewParent;
		public TextView mTextView;
		public CheckBox mCheckBox;
	}

	private void setSingleChoiceSelected() {
		if (mdialog.isShowing())
			mdialog.dismiss();
	}

	// public void refreshData(List<String> objects) {
	// if (objects != null) {
	// mSingleDataNameList = objects;
	// setSelectItem(0);
	// }
	// }
	//
	public void setSelectItem(int selectItem) {
		if (selectItem >= 0 && selectItem < mFirstList.size()) {
			mSelectItem = selectItem;
			notifyDataSetChanged();
		}
	}

	public int getSelectItem() {
		return mSelectItem;
	}
	//
	// public void clear() {
	// mSingleDataNameList.clear();
	// notifyDataSetChanged();
	// }

}
